package com.zmzncs.lmtc.module.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.common.pojo.LoginUser;
import com.zmzncs.lmtc.common.pojo.daoer.CreateMonth;
import com.zmzncs.lmtc.common.pojo.tengda.AllMonthCardInfo;
import com.zmzncs.lmtc.common.pojo.tengda.CarInfo;
import com.zmzncs.lmtc.common.pojo.tengda.CarTypes;
import com.zmzncs.lmtc.common.pojo.tengda.MonthCardForm;
import com.zmzncs.lmtc.common.util.JwtUtil;
import com.zmzncs.lmtc.common.util.TengdaUtil;
import com.zmzncs.lmtc.module.customer.dao.CardDao;
import com.zmzncs.lmtc.module.customer.pojo.card.Card;
import com.zmzncs.lmtc.module.customer.pojo.month_record.MonthRecord;
import com.zmzncs.lmtc.module.customer.service.CardService;
import com.zmzncs.lmtc.module.customer.service.MonthRecordService;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfo;
import com.zmzncs.lmtc.module.park.service.ParkInfoDaoerService;
import com.zmzncs.lmtc.module.promotion.dao.MonthRulePullDao;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule_pull.MonthRulePullDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 客户-会员卡 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-09-02
 */
@Slf4j
@Service
public class CardServiceImpl extends ServiceImpl<CardDao, Card> implements CardService {

    @Autowired
    ParkInfoDaoerService parkInfoDaoerService;
    @Autowired
    MonthRecordService monthRecordService;
    @Autowired
    ParkInfoDao parkInfoDao;
    @Autowired
    MonthRulePullDao monthRulePullDao;


    @Autowired
    HttpServletRequest request;


    @Override
    public Map<String, String> parkCreatMonth(CreateMonth createMonth) {
        Map<String, String> map = new HashMap<>();

        //获取登录人信息
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        createMonth.setParkManagerId(loginUser.getCustomerId());

        Integer parkSource = createMonth.getParkSource();

        if (parkSource == 1){
            map.put("status","1");
            map.put("message","此停车场暂不支持添加月租车辆");
            log.info("此停车场暂不支持添加月租车辆");
        }

        //道尔停车场
        if (parkSource == 3){
            //道尔月卡开户
            map = parkInfoDaoerService.daoerCreatMonth(createMonth);
        }

        //腾达停车场
        if (parkSource == 4){
            map = tengdaMonth(createMonth);
        }
        return map;
    }

    @Override
    public List<CarTypes> getCarTypes(Long parkInfoId,Integer parkSource) {
        List<CarTypes> carTypesList = new ArrayList<>();
        ParkInfo parkInfo = parkInfoDao.selectById(parkInfoId);
        //道尔
        if (parkSource == 3){
            List<MonthRulePullDTO> monthRulePullDTOList = monthRulePullDao.getMonthRulePullByParkInfoId(parkInfoId);
            if (monthRulePullDTOList != null && monthRulePullDTOList.size() > 0){
                for (MonthRulePullDTO rule : monthRulePullDTOList) {
                    CarTypes carTypes = new CarTypes();
                    carTypes.setCode(Integer.valueOf(rule.getMonthType()));
                    String monthName = rule.getMonthName();
                    String durationType = rule.getDurationType();
                    if ("1".equals(durationType)){
                        monthName = monthName + "(月租)";
                    } else if ("2".equals(durationType)){
                        monthName = monthName + "(季租)";
                    } else if ("3".equals(durationType)){
                        monthName = monthName + "(半年租)";
                    } else {
                        monthName = monthName + "(年租)";
                    }
                    carTypes.setName(monthName);
                    carTypesList.add(carTypes);
                }
            }
        }
        //腾达
        if (parkSource == 4){
            List<CarTypes> carTypes = TengdaUtil.getCarTypes(parkInfo.getBolianParkId());
            if (carTypes != null && carTypes.size() > 0){
                for (CarTypes carType : carTypes) {
                    if (carType.getCode() >= 81 && carType.getCode() <= 89){
                        carTypesList.add(carType);
                    }
                }
            }
        }
        return carTypesList;
    }

    /**
     * 腾达月租充值
     * @param createMonth
     * @return
     */
    public Map<String,String> tengdaMonth(CreateMonth createMonth){
        Map<String, String> map = new HashMap<>();
        int i = 0;

        ParkInfo parkInfo = parkInfoDao.selectById(createMonth.getParkInfoId());
        String bolianParkId = parkInfo.getBolianParkId();

        createMonth.setParkId(bolianParkId);
//        createMonth.setCardTypeId(11);
        createMonth.setPayType(0);
        createMonth.setContactName("停车场主");

        String carNo = createMonth.getCarNo();

        MonthCardForm form = new MonthCardForm();
        form.setBolianParkId(bolianParkId);
        form.setCarMMoney(Double.valueOf(createMonth.getBalanceMoney()));
        form.setTimeStart(createMonth.getStartTime());
        form.setTimeEnd(createMonth.getEndTime());
        form.setCarTypeCode(createMonth.getCardTypeId());
        form.setCarNum(carNo);

        //查询之前是否购买月卡
        AllMonthCardInfo carMonthCard = TengdaUtil.getCarMonthCard(bolianParkId, carNo);
        if (carMonthCard != null){
            List<CarInfo> carInfoList = carMonthCard.getItems();
            if (carInfoList != null && !carInfoList.isEmpty()) {
                form.setUuid(carInfoList.get(0).getUUID());
                form.setCarTypeCode(Integer.valueOf(carInfoList.get(0).getCarTypeCode()));

                //续卡
                Boolean isSuccess = TengdaUtil.renewalMonthCard(form);
                if (isSuccess){
                    int j = monthRecordService.addMonthRecord(createMonth);
                    map.put("status","1");
                    map.put("message","车辆月租充值成功");
                    log.info("腾达车月租续卡成功");
                } else {
                    map.put("status","0");
                    map.put("message","车辆月租充值失败");
                    log.info("腾达月租续卡失败");
                }
            }
        } else {
            //购买月卡  开户
            Boolean aBoolean = TengdaUtil.addMonthCard(form);
            if (aBoolean){
                int j = monthRecordService.addMonthRecord(createMonth);
                map.put("status","1");
                map.put("message","车辆月租充值成功");
                log.info("腾达车月租开户成功");
            } else {
                map.put("status","0");
                map.put("message","车辆月租充值失败");
                log.info("腾达月租开户失败");
            }
        }
        return map;
    }




}
